/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.resources.StyleSheetReference;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.version.undo.Change;

/**
 *
 * @author JKorreck
 */
public class MyItems extends WebPage implements PropertyChangeListener {

    String selectedID;

    public MyItems() {



        MyItemManager.getInstance().addPCL(this);
        add(new Label("selectedLabel", new PropertyModel(this, "selectedItemLabel")));
//        add(new Label("editLabel", new PropertyModel(this, "editItemLabel")));

        add(new Label("title", "One Item to add"));
        add(new StyleSheetReference("stylesheet", MyItems.class, "style.css"));
        this.addListView();

//        this.add(new Link("addItemLink") {
//
//            @Override
//            public void onClick() {
//                MyItems.this.setResponsePage(AddItem.class);
//            }
//        });

        this.setOutputMarkupId(true);
//        add(itemForm);

        // Test

        List<IColumn> columns = new ArrayList<IColumn>();

        columns.add(new AbstractColumn(new Model("Actions")) {

            public void populateItem(Item cellItem, String componentId, IModel model) {
                cellItem.add(new ItemActionPanel(componentId, model, MyItems.this));
            }
        });

        columns.add(new PropertyColumn(new Model("ID"), "id", "id") {
//            @Override
//            public String getCssClass()
//            {
//                return "numeric";
//            }
        });

        columns.add(new PropertyColumn(new Model("Category"), "category", "category") {
//            @Override
//            public String getCssClass()
//            {
//                return "numeric";
//            }
        });

        columns.add(new PropertyColumn(new Model("Name"), "name", "name"));

//        columns.add(new PropertyColumn(new Model("Last Name"), "lastName", "lastName")
//        {
//            @Override
//            public String getCssClass()
//            {
//                return "last-name";
//            }
//        });

        columns.add(new PropertyColumn(new Model("Weight"), "weight"));
        columns.add(new PropertyColumn(new Model("Unit"), "unit"));
        columns.add(new PropertyColumn(new Model("Count"), "count"));
        columns.add(new PropertyColumn(new Model("Price"), "price"));

        columns.add(new AbstractColumn(new Model("Actions2")) {

            public void populateItem(Item cellItem, String componentId, IModel model) {
                cellItem.add(new InDecreasePanel(componentId, model, MyItems.this));
            }
        });

        add(new DefaultDataTable("table", columns, new SortableItemsProvider(), 10));

        add(new Link("backLink") {

            @Override
            public void onClick() {
                setResponsePage(StartPage.class);
            }
        });
    }

    private void addListView() {
        List<MyItem> allItems = MyItemManager.getInstance().getAllItems();
        add(new ListView("listview", allItems) {

            protected void populateItem(ListItem item) {
                MyItem actItem = (MyItem) item.getModelObject();

                item.add(new Label("id", actItem.getId()));
                item.add(new Label("category", actItem.getCategory()));
                item.add(new Label("name", actItem.getName()));
                item.add(new Label("weight", "" + actItem.getWeight()));
                item.add(new Label("unit", "" + actItem.getUnit()));
                item.add(new Label("count", "" + actItem.getCount()));
                item.add(new Label("price", "" + actItem.getPrice()));
            }
        });
    }

    public String getSelected() {
        return selectedID;
    }

    public void setSelected(MyItem selected) {
        addStateChange(new Change() {

            private final String old = MyItems.this.selectedID;

            public void undo() {
                MyItems.this.selectedID = old;

            }
        });
        this.selectedID = selected.getId();
    }

    public String getSelectedItemLabel() {
        if (selectedID == null) {
            return "No Item Selected";
        } else {
            return selectedID.toString();
        }
    }

    public void editSelectedItem(MyItem editItem) {
//        this.itemForm.setModelObject(editItem);
//        ((TextField) this.itemForm.get("id")).setEnabled(false);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        this.setResponsePage(MyItems.class);
    }
}
